package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.accounting.entity.Subject;
import cc.mrbird.febs.accounting.service.ISubjectService;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 *  Controller
 *
 * @author baoyinlei
 * @date 2021-09-28 23:26:51
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class SubjectController extends BaseController {

    private final ISubjectService subjectService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "subject")
    public String subjectIndex(){
        return FebsUtil.view("subject/subject");
    }

    @GetMapping("subject/{projectId}")
    @ResponseBody
    @RequiresPermissions("project:list")
    public FebsResponse getAllSubjects(Subject subject, @NotNull(message = "{required}") @PathVariable Long projectId) {
        return new FebsResponse().success().data(subjectService.findSubjects(projectId));
    }

    @GetMapping("subject/list/{projectId}")
    @ResponseBody
    @RequiresPermissions("project:list")
    public FebsResponse subjectList(QueryRequest request, @NotNull(message = "{required}") @PathVariable Long projectId) {
        Map<String, Object> dataTable = getDataTable(this.subjectService.findSubjects(request, projectId));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Subject", exceptionMessage = "新增Subject失败")
    @PostMapping("subject")
    @ResponseBody
    @RequiresPermissions("project:add")
    public FebsResponse addSubject(@Valid Subject subject) {
        this.subjectService.createSubject(subject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Subject", exceptionMessage = "删除Subject失败")
    @GetMapping("subject/delete/{subjectId}")
    @ResponseBody
    @RequiresPermissions("project:delete")
    public FebsResponse deleteSubject(@NotBlank(message = "{required}") @PathVariable String subjectId) {
        this.subjectService.deleteSubject(subjectId);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Subject", exceptionMessage = "修改Subject失败")
    @PostMapping("subject/update")
    @ResponseBody
    @RequiresPermissions("project:update")
    public FebsResponse updateSubject(Subject subject) {
        this.subjectService.updateSubject(subject);
        return new FebsResponse().success();
    }
}
